<template>
  <el-dialog v-model="dialogFormVisible" title='撤回出库申请' width="30%" center>
    <span>您确定再次申请吗?</span>
    <template #footer>
      <span class="dialog-footer">
        <el-button @click="dialogFormVisible = false">
          取消
        </el-button>
        <el-button type="primary" @click="goApplyAgain">
          确定
        </el-button>
      </span>
    </template>
  </el-dialog>
</template>

<script lang="ts" setup>
import { onBeforeUnmount, ref, reactive } from 'vue'
import { bus } from "../../../utils/mitt.js"
import { ApplyProductOutAPI } from '../../../api/product.js'
import { ElMessage } from 'element-plus'
import { tracking } from '../../../utils/operate'



const props = defineProps<{
  refreshData: Function
}>()

bus.on('againInfo', (row: any) => {

  formData.id = row.id
  formData.product_in_warehouse_number = row.product_in_warehouse_number
  formData.product_name = row.product_name
  formData.product_single_price = row.product_single_price
  formData.product_out_number = row.product_out_number
  formData.product_out_id = row.product_out_id
})

interface FormData {
  id: number | null,
  product_name: string | null,
  product_out_id: number | null | string,
  product_in_warehouse_number: number | null,
  product_single_price: number | null,
  product_out_number: number | null | string,
  product_out_apply_person: string,
  apply_memo: string,
}

const formData: FormData = reactive({
  id: null,
  product_name: null,
  product_out_id: null,
  product_in_warehouse_number: null,
  product_single_price: null,
  product_out_number: null,
  product_out_apply_person: JSON.parse(localStorage.getItem('userinfo') as string).name,
  apply_memo: ''
})



const goApplyAgain = async () => {

  const res = await ApplyProductOutAPI(formData)
  if (res.data.status == 0) {
    ElMessage({
      message: '再次申请出库成功',
      type: 'success',
    })
    props.refreshData()
    dialogFormVisible.value = false
    tracking("产品管理", "再次申请出库", { name: formData.product_name, number: formData.product_out_number },"中级")
  } else {
    ElMessage.error('再次申请出库失败')
    dialogFormVisible.value = false
  }
}

// 弹窗开关
const dialogFormVisible = ref(false)

// 打开编辑管理员的弹窗
const open = () => {
  dialogFormVisible.value = true
}

defineExpose({
  open
})

onBeforeUnmount(() => {
  bus.all.clear()
})
</script>

<style lang="scss" scoped></style>